# coding: utf-8

# -------------------------------------------------------------------------------
# Name:         validator.py
# Description:
# Author:       XiangjunZhao
# EMAIL:        2419352654@qq.com
# Date:         2019/11/18 17:32
# -------------------------------------------------------------------------------
import logging

from apps.HttpAutoTestService.core.builtin.comparator_dict import comparator_dict
from apps.HttpAutoTestService.core.extractor import Extractor
from apps.HttpAutoTestService.core.loader import load_built_in_comparators

logger = logging.getLogger(__name__)


class Validator(object):
    """
    校验器
    主要功能：
        1、格式化校验变量
        2、校验期望结果与实际结果与预期一致，并返回校验结果
    """

    def __init__(self):
        self.validate_variables_list = []

    def uniform_validate(self, validate_variables):
        """
        统一格式化测试用例的验证变量validate
        Args:
            validate_variables: 参数格式 list、dict
                示例：
                    [{"check":"result.user.name","comparator":"eq","expect":"XianjunZhao"}]
                    or {"check":"result.user.name","comparator":"eq","expect":"XianjunZhao"}

        Returns: 返回数据格式 list
                示例：
                    [{"check":"result.user.name","comparator":"eq","expect":"XianjunZhao"}]

        """
        if isinstance(validate_variables, list):
            for item in validate_variables:
                self.uniform_validate(item)
        elif isinstance(validate_variables, dict):
            if "check" in validate_variables.keys() and "expect" in validate_variables.keys():
                # 如果验证mapping中不包含comparator时，默认为{"comparator": "eq"}
                check_item = validate_variables.get("check")
                expect_value = validate_variables.get("expect")
                comparator = validate_variables.get("comparator", "eq")
                self.validate_variables_list.append({
                    "check": check_item,
                    "expect": expect_value,
                    "comparator": comparator
                })
        else:
            logger.error("参数格式错误！")

    def validate(self, resp_obj=None):
        """
        校验期望结果与实际结果与预期一致
        Args:
            resp_obj: ResponseObject对象实例

        Returns:

        """

        validate_pass = "PASS"
        built_in_comparators = load_built_in_comparators()

        # 记录校验失败的原因
        failure_reason = []
        for validate_variable in self.validate_variables_list:
            check_item = validate_variable['check']
            expect_value = validate_variable['expect']
            comparator = validate_variable['comparator']
            actual_value = Extractor.extract_value_by_jsonpath(resp_obj=resp_obj, expr=check_item)
            try:
                # 获取比较器
                fun = built_in_comparators[comparator]
                fun(actual_value=actual_value, expect_value=expect_value)
            except (AssertionError, TypeError):
                validate_pass = "FAIL"
                failure_reason.append({
                    '检查项': check_item,
                    '期望值': expect_value,
                    '实际值': actual_value,
                    '断言方法': comparator_dict.get(comparator),
                })
        return validate_pass, failure_reason
